﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using control.Biz;
using System.Collections.Generic;

public partial class usercontrol_category : System.Web.UI.UserControl
{
    #region page property
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];//cookie viewstate
            int _CurrentPage = 0;                      //code lay gia try cookie bo vao currentpage neu ko tra ve 0
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get//code lay gia try cookie bo vao firstindex neu ko tra ve 0
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get//code lay gia try cookie bo vao lastindex neu ko tra ve 0
        {

            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }
    PagedDataSource _PageDataSource = new PagedDataSource();
    #endregion
    #region property
    private List<BizSach> saches;
    public List<BizSach> Saches
    {
        get
        {
            if (saches == null)
                saches = new List<BizSach>();
            return saches;
        }
        set { saches = value; }

    }
    private string tieuDe;
    public string TieuDe
    {
        get { return tieuDe; }
        set { tieuDe = value; }
    }
    
    #endregion
    #region code page
    private void BindItemsList()
    {
        _PageDataSource.DataSource = saches;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 12;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        //this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage; //hay hay thay cho if
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.dlSach.DataSource = _PageDataSource;
        this.dlSach.DataBind();
        this.doPaging();
    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        fistIndex = 0;
        lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
        //fistIndex = CurrentPage - 5;
        //if (CurrentPage > 5)
        //{
        //    lastIndex = CurrentPage + 5;
        //}
        //else
        //{
        //    lastIndex = 10;
        //}
        //if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        //{
        //    lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
        //    fistIndex = lastIndex - 10;
        //}
        //if (fistIndex < 0)
        //{
        //    fistIndex = 0;
        //}
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        this.DataList1.DataSource = dt;
        this.DataList1.DataBind();
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbTitle.Text = TieuDe;
            try 
            { 
                lbnodata.Text = saches[0].TenChuDe;
                lbnodata.Text = "";
            }
            catch { lbnodata.Text = "No Data"; }
            BindItemsList();
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.BindItemsList();
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;

        }
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        this.BindItemsList();
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        this.BindItemsList();
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        this.BindItemsList();
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = Convert.ToInt32(ViewState["TotalPages"]) - 1;
        this.BindItemsList();
    }
}
